package com.example.demo.controller;

import com.example.demo.pojo.Order.CreatOrderPojo;
import com.example.demo.pojo.Order.Orders;
import com.example.demo.pojo.Order.PutOrders;
import com.example.demo.pojo.R;
import com.example.demo.service.OrderService;
import com.example.demo.utils.TokenUtil.JwtUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private OrderService orderService;

    /**
     * 创建订单接口
     *
     * @return 购买结果
     */
    @PostMapping("/createOrder")
    public R<T> createOrder(@RequestBody CreatOrderPojo creatOrderPojo,
                            @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return orderService.creatOrder(token, creatOrderPojo);
        } catch (Exception e) {
            logger.error("创建订单失败", e);
            return R.failed("创建订单失败");
        }
    }

    /**
     * 新创建订单接口
     *
     * @return 购买结果
     */
    @PostMapping("/createOrderNew")
    public R<T> createOrderNew(@RequestBody CreatOrderPojo creatOrderPojo,
                            @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return orderService.creatOrderNew(token, creatOrderPojo);
        } catch (Exception e) {
            logger.error("创建订单失败", e);
            return R.failed("创建订单失败");
        }
    }

    /**
     * 查询用户在此比赛中所有挂单记录（包含未成交和已成交以及撤单）。
     *
     * @param id    比赛id
     * @param token 用户登录时获取的Token
     * @return 挂单记录
     */
    @GetMapping("/getPutOrder/{id}")
    public R<List<PutOrders>> getPutOrderList(@PathVariable Integer id,
                                              @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return orderService.getPutOrderList(token, id);
        } catch (Exception e) {
            logger.error("获取挂单记录失败", e);
            return R.failed("获取挂单记录失败");
        }
    }

    /**
     * 查询用户在此比赛中所有挂单记录（包含未成交和已成交以及撤单）。
     *
     * @param competitionId    比赛id
     * @param orderStatus 订单状态
     * @param buyMethod 购买方式
     * @param token 用户登录时获取的Token
     * @return 挂单记录
     */
    @GetMapping("/getOrderList")
    public R<List<Orders>> getOrderList(@RequestParam Integer competitionId,
                                        @RequestParam String orderStatus,
                                        @RequestParam String buyMethod,
                                        @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return orderService.getOrderList(token, competitionId, orderStatus ,buyMethod);
        } catch (Exception e) {
            logger.error("获取挂单记录失败", e);
            return R.failed("获取挂单记录失败");
        }
    }

    /**
     * 撤单接口
     *
     * @param orderId   订单id
     * @param competitionId    比赛id
     * @param withdrawNumber   撤单数量
     * @param orderStatus    订单状态
     * @param token 用户登录时获取的Token
     * @return 撤单是否成功
     */
    @GetMapping("/withDrawOrder")
    public R<T> withDrawOrder(
                @RequestParam Integer orderId,
                @RequestParam Integer competitionId,
                @RequestParam Integer withdrawNumber,
                @RequestParam String orderStatus,
                @CookieValue(name = "token", required = false) String token) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }

        try {
            return orderService.withdrawOrder(token,orderId,competitionId,withdrawNumber,orderStatus);
        } catch (Exception e) {
            logger.error("撤单失败", e);
            return R.failed("撤单失败");
        }
    }

}
